package com.example.bailin.giftsay.guide.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.HomeCellEntity;
import com.example.bailin.giftsay.guide.NickNListener;
import com.example.bailin.giftsay.guide.PopupClick;
import com.example.bailin.giftsay.guide.StoreLvListener;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

/**
 * Created by bailin on 16/8/16.
 * <p>
 * 指南里所有listview的item赋值的适配器
 */
public class SelectionAdapter extends BaseAdapter {

    private HomeCellEntity homeCellEntity;

    private PopupClick popupClick;
    private LmClickListener lmClickListener;
    private NickNListener nickNListener;
    private StoreLvListener storeLvListener;

    public void setStoreLvListener(StoreLvListener storeLvListener) {
        this.storeLvListener = storeLvListener;
    }

    public void setNickNListener(NickNListener nickNListener) {
        this.nickNListener = nickNListener;
    }

    public void setLmClickListener(LmClickListener lmClickListener) {
        this.lmClickListener = lmClickListener;
    }

    public void setPopupClick(PopupClick popupClick) {
        this.popupClick = popupClick;
    }

    public void setHomeCellEntity(HomeCellEntity homeCellEntity) {
        this.homeCellEntity = homeCellEntity;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return homeCellEntity.getData().getItems() == null ? 0 : homeCellEntity.getData().getItems().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(MyApp.getContext());
        MyViewHolder myViewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview_selection, parent, false);

            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {

            myViewHolder = (MyViewHolder) convertView.getTag();
        }


        NetTool tool = new NetTool();
        try {
            tool.getImg(homeCellEntity.getData().getItems().get(position).getCover_image_url(), myViewHolder.imageuUrlSelection);
            tool.getRoundImg(homeCellEntity.getData().getItems().get(position).getAuthor().getAvatar_url(), myViewHolder.ivAvatarUrl);
            myViewHolder.tvTitle.setText(homeCellEntity.getData().getItems().get(position).getTitle());
            myViewHolder.tvCategory.setText(homeCellEntity.getData().getItems().get(position).getColumn().getCategory());
            myViewHolder.tvColumnTitle.setText(homeCellEntity.getData().getItems().get(position).getColumn().getTitle());
            myViewHolder.tvNicKName.setText(homeCellEntity.getData().getItems().get(position).getAuthor().getNickname());
            myViewHolder.tvLikesCount.setText("" + homeCellEntity.getData().getItems().get(position).getLikes_count());
        } catch (NullPointerException e) {

        }


        myViewHolder.imageuUrlSelection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupClick.itemOnClick(position);
            }
        });

        myViewHolder.tvColumnTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lmClickListener.onClick
                        (homeCellEntity.getData().getItems().get(position).getColumn().getId()
                                , homeCellEntity.getData().getItems().get(position).getColumn().getTitle());
            }
        });

        myViewHolder.ivAvatarUrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nickNListener.onClick
                        (homeCellEntity.getData().getItems().get(position).getAuthor().getId()
                                , homeCellEntity.getData().getItems().get(position).getAuthor().getAvatar_url()
                                , homeCellEntity.getData().getItems().get(position).getAuthor().getNickname());
            }
        });

        myViewHolder.ivItemSelectionAssess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                storeLvListener.onClick(homeCellEntity, position);
            }
        });
        return convertView;
    }

    private class MyViewHolder {
        private ImageView imageuUrlSelection;
        private ImageView ivAvatarUrl;
        private TextView tvTitle;
        private TextView tvCategory;
        private TextView tvColumnTitle;
        private TextView tvNicKName;
        private TextView tvLikesCount;
        private ImageView ivItemSelectionAssess;

        public MyViewHolder(View v) {
            imageuUrlSelection = (ImageView) v.findViewById(R.id.cover_image_url_item_selection);
            tvTitle = (TextView) v.findViewById(R.id.title_item_selection);
            tvCategory = (TextView) v.findViewById(R.id.tv_category);
            tvColumnTitle = (TextView) v.findViewById(R.id.tv_column_title);
            tvNicKName = (TextView) v.findViewById(R.id.tv_nickname);
            ivAvatarUrl = (ImageView) v.findViewById(R.id.iv_avatar_url);
            tvLikesCount = (TextView) v.findViewById(R.id.tv_item_selection_number);
            ivItemSelectionAssess = (ImageView) v.findViewById(R.id.iv_item_selection_assess);
        }
    }

}
